import context from '../main.js'

export function getQueryVariable(variable){
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i=0;i<vars.length;i++) {
        var pair = vars[i].split("=");
        if(pair[0] == variable){return pair[1];}
    }
    return '';
}

//判断字符是否为空的方法
export function isEmpty(obj) {
	if (typeof obj == "undefined" || obj == null || obj == "") {
		return true;
	} else {
		return false;
	}
}

export function formatImageUrl(imgUrl, width, height) {
	if (width > 0 && height > 0) {
		var w = ',w_240';
		var h = ',h_240';

		imgUrl += '?x-oss-process=image/resize' + w + h;
	}
	return imgUrl;
}

export function wrapVideoSnapshot(videoUrl) {
	return videoUrl += '?x-oss-process=video/snapshot,t_0,f_jpg,m_fast';
}

export function sectionToChinese(num) {
	let changeNum = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']; //changeNum[0] = "零"
	let unit = ["", "十", "百", "千", "万"];
	num = parseInt(num);
	let getWan = (temp) => {
		let strArr = temp.toString().split("").reverse();
		let newNum = "";
		for (var i = 0; i < strArr.length; i++) {
			newNum = (i == 0 && strArr[i] == 0 ? "" : (i > 0 && strArr[i] == 0 && strArr[i - 1] == 0 ? "" : changeNum[strArr[i]] +
				(strArr[i] == 0 ? unit[0] : unit[i]))) + newNum;
		}
		return newNum;
	}
	let overWan = Math.floor(num / 10000);
	let noWan = num % 10000;
	if (noWan.toString().length < 4) noWan = "0" + noWan;
	return overWan ? getWan(overWan) + "万" + getWan(noWan) : getWan(num);
}

export function formatDateByTimestamp(timestamp, fmt) {
	return context.$u.timeFormat(timestamp, fmt);
};

export function formatDate(datetime, fmt) {
	return context.$u.timeFormat(datetime, fmt);
};

export  function getWeekFirstDay (date) {
    let day = date.getDay() || 7;
	console.log(day);
    return new Date(date.getFullYear(), date.getMonth(), date.getDate() + 1 - day);
};

export function getWeekLastDay(date) {
	let day = date.getDay() || 7;
	console.log(day);
	return new Date(date.getFullYear(), date.getMonth(), date.getDate() + 7 - day);
}


export function wrapMonthByBirthday(birthday) {

	const d1 = new Date(birthday);
	const d2 = new Date();//日期2
	const year = d2.getFullYear() - d1.getFullYear();
	let month = d2.getMonth() - d1.getMonth();
	const days = d2.getDate() - d1.getDate();
	// console.log(year);
	// console.log(month);
	// console.log(days);
	month += (year * 12);
	return days < 0 ? month -1 : month;
}

export const transferFormData = (obj, formData) => {
  const data = []
  if (obj instanceof Array) {
    for (let j = 0, len = obj.length; j < len; j++) {
      const arr = transferFormData(obj[j])
      for (let k = 0, l = arr.length; k < l; k++) {
        const key = formData ? j + arr[k].key : '[' + j + ']' + arr[k].key
        data.push({ key: key, value: arr[k].value })
      }
    }
  } else if (typeof obj === 'object') {
    for (const j in obj) {
      if (obj[j] !== null && obj[j] !== undefined) {
        const arr = transferFormData(obj[j])
        for (let k = 0, l = arr.length; k < l; k++) {
          const key = formData ? j + arr[k].key : '.' + j + '' + arr[k].key
          data.push({ key: key, value: arr[k].value })
        }
      }
    }
  } else {
    data.push({ key: '', value: obj })
  }
  if (formData) {
    // 封装
    for (let i = 0, len = data.length; i < len; i++) {
      formData.append(data[i].key, data[i].value)
    }
  } else {
    return data
  }
}